﻿using System;
using System.Xml;
using System.Xml.Linq;
using com.IronOne.BoardPACWinAppBO.General;
using com.IronOne.BoardPACWinAppUtil.Enums;
using com.IronOne.BoardPACWinAppUtil.Util;

namespace com.IronOne.BoardPACWinAppService.Xml
{
    internal class XmlParser
    {
        /// <summary>
        ///     Validate your XML HTTP Response
        /// </summary>
        /// <param name="xml">HTTP Response string</param>
        /// <returns>ResponseDataModel</returns>
        public static ResponseDataModel Parse(string xml)
        {
            var rdm = new ResponseDataModel();
            try
            {
                var xDocument = XDocument.Parse(xml.Trim());
                var xResponse = xDocument.Element("Response");
                var xHeader = xResponse.Element("Header");
                var xStatus = xHeader.Element("Status");

                #region Status

                if (!(xStatus.Element("code").Value.Trim().Equals("0")))
                {
                    rdm.IsError = true;
                    rdm.ErrorCode = xStatus.Element("code").Value.Trim();
                    rdm.ErrorMessage = xStatus.Element("description").Value.Trim();
                    return rdm;
                }

                #endregion

                #region Tracking

                var trackElement = xHeader.Element("Tracking");
                if (trackElement != null)
                {
                    rdm.ServerId = int.Parse(trackElement.Element("ServerId").Value.Trim());
                    rdm.ServerTime = Utility.GetDate(trackElement.Element("ServerTime").Value.Trim());
                    rdm.WebAppVersion = trackElement.Element("WebAppVersion").Value.Trim();
                    rdm.CooperateId = int.Parse(trackElement.Element("CooperateId").Value.Trim());
                }

                #endregion

                #region Data

                rdm.Data = xResponse.Element("Data").ToString().Trim();

                #endregion
            }
            catch (NullReferenceException)
            {
                rdm.IsError = true;
                rdm.ErrorMessage = "XML tags are wrong or missing";
            }
            catch (XmlException)
            {
                rdm.IsError = true;
                rdm.ErrorMessage = "XML parse failed";
            }
            catch (Exception)
            {
                rdm.IsError = true;
                rdm.ErrorMessage = "XML parse General failure";
            }
            return rdm;
        }

        public static DeviceStatus GetDeviceAuthorizationStatus(string xml)
        {
            try
            {
                var xDocument = XDocument.Parse(xml.Trim());
                var xResponse = xDocument.Element("response");
                var devStat = Convert.ToInt32(xResponse.Element("Data").Element("DeviceStatus").ToString().Trim());

                var dStat = (DeviceStatus) devStat;
                return dStat;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}